<?php
namespace App\Http\Controllers\Login;
use App\Http\Controllers\Controller;
use App\Models\UserModels\Userinfo;
use Illuminate\Http\Request;
use App\Http\Controllers\ApiBaseController;
class UserinfoController extends ApiBaseController{
	
	private $thirsdUrl="";
	private $thirsdUrlTwo="";
	
	public function __construct(Request $request){
		parent::__construct($request);
        $this->parames = $request->all();
		$this->thirsdUrl="http://api.xstatus.cn/douyin/video/comments?token=3440488a80744d30abe1a41f3a7062d5&aweme_id=";
		$this->thirsdUrlTwo="http://api.xstatus.cn/douyin/video/comments/reply?token=3440488a80744d30abe1a41f3a7062d5&aweme_id=";
    }
	
	//主页用户
	function info(){
		return view('login/userinfo/info',['user_list_info'=>true]);
	}
	
	//视频用户
	function index(){
		return view('login/userinfo/index',['user_list_index'=>true]);
	}

    //直播用户
    function live(){
        return view('login/userinfo/live',['user_list_live'=>true]);
    }

    //广告用户
    function advert(){
        return view('login/userinfo/advert',['user_list_advert'=>true]);
    }
	//ajax获取数据
	function dataList(){
		$userInfo=new Userinfo();
		$result=$userInfo->selectData($this->parames);
		exit(json_encode($result));
	}


	//添加主页分享数据
	function addinfo(){
		$url=isset($this->parames['info_url']) ? $this->parames['info_url'] : '';
		!$url ? $this->returnJson(1,'请输入主页分享链接地址') : '';
		//取出地址
		$pgma="/http(s):\/\/v.douyin.com\/[a-zA-z0-9]{3,}\//";
		preg_match($pgma,$url,$urlArr);
		$info_vurl=isset($urlArr[0]) && $urlArr[0] ? $urlArr[0] : $this->returnJson(1,'网址错误');//短链接
		
		$real_url=$this->getContents($info_vurl);//实际网页链接
		!$real_url ? $this->returnJson(1,'网络繁忙') : '';
		
		$sec_uid="sec_uid=".$this->cut_str($real_url,'/user/','?',6);//取出主页sec_uid
		
		$getUrl="https://www.iesdouyin.com/web/api/v2/user/info/?".$sec_uid;//接口地址
		$userInfoData=$this->douyinContents($getUrl);
		
		$system_type=isset(session('user')[0]['role_id']) ? intval(session('user')[0]['role_id']) : 1;
		$partner_id=isset(session('user')[0]['admin_id']) ? intval(session('user')[0]['admin_id']) : 0;
		$data=array(
			'info_id'=>isset($userInfoData['user_info']['unique_id']) && !empty($userInfoData['user_info']['unique_id']) ? $userInfoData['user_info']['unique_id'] : $userInfoData['user_info']['short_id'],
			'info_name'=>isset($userInfoData['user_info']['nickname']) ? htmlspecialchars($userInfoData['user_info']['nickname'],ENT_QUOTES) : '',
			'info_img'=>isset($userInfoData['user_info']['avatar_thumb']['url_list'][0]) ? $userInfoData['user_info']['avatar_thumb']['url_list'][0] : '',
			'info_signature'=>isset($userInfoData['user_info']['signature']) ? htmlspecialchars($userInfoData['user_info']['signature'],ENT_QUOTES) : '',
			'info_follow'=>isset($userInfoData['user_info']['following_count']) ? $userInfoData['user_info']['following_count'] : '',
			'info_fans'=>isset($userInfoData['user_info']['follower_count']) ? $userInfoData['user_info']['follower_count'] : '',
			'info_support'=>isset($userInfoData['user_info']['total_favorited']) ? $userInfoData['user_info']['total_favorited'] : '',
			'info_opus'=>isset($userInfoData['user_info']['aweme_count']) ? $userInfoData['user_info']['aweme_count'] : '',
			'info_url'=>$getUrl,
			'info_vurl'=>$info_vurl,
			'info_date'=>date('Y-m-d H:i:s',time())
		);
		// echo "<pre/>";print_r($userInfoData);exit();
		$userInfo=new Userinfo();
		$values=array(1,$sec_uid,$system_type,$partner_id);
		$returnId=$userInfo->insertUser($data,$values);
		
		$returnId ? $this->returnJson(0,'操作成功') : $this->returnJson(1,'操作失败');
	}
	
	//添加视频分享数据
	function addindex(){
		$url=isset($this->parames['info_url']) ? $this->parames['info_url'] : '';
		!$url ? $this->returnJson(1,'请输入视频分享链接地址') : '';
		//取出地址
		$pgma="/http(s):\/\/v.douyin.com\/[a-zA-z0-9]{3,}\//";
		preg_match($pgma,$url,$urlArr);
		$info_vurl=!empty($urlArr[0]) ? $urlArr[0] : '';//短链接
		!$info_vurl ? $this->returnJson(1,'网址错误') : '';
		
		$real_url=$this->getContents($info_vurl);//实际网页链接
		!$real_url ? $this->returnJson(1,'网络繁忙') : '';
		
		$item_ids=$this->cut_str($real_url,'/video/','?',7);//取出 /video/  下的数字 ID
		
		$getUrl="https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=".$item_ids;//接口地址
		$userInfoData=$this->douyinContents($getUrl);
		
		$system_type=isset(session('user')[0]['role_id']) ? intval(session('user')[0]['role_id']) : 1;
		$partner_id=isset(session('user')[0]['admin_id']) ? intval(session('user')[0]['admin_id']) : 0;
		$data=array(
			'info_id'=>isset($userInfoData['item_list'][0]['author']['unique_id']) && !empty($userInfoData['item_list'][0]['author']['unique_id']) ? $userInfoData['item_list'][0]['author']['unique_id'] : $userInfoData['item_list'][0]['author']['short_id'],
			'info_name'=>isset($userInfoData['item_list'][0]['author']['nickname']) ? htmlspecialchars($userInfoData['item_list'][0]['author']['nickname'],ENT_QUOTES) : '',
			'info_img'=>isset($userInfoData['item_list'][0]['video']['cover']['url_list'][0]) ? $userInfoData['item_list'][0]['video']['cover']['url_list'][0] : '',
			'info_support'=>isset($userInfoData['item_list'][0]['statistics']['digg_count']) ? $userInfoData['item_list'][0]['statistics']['digg_count'] : '',
			'info_opus'=>isset($userInfoData['item_list'][0]['statistics']['comment_count']) ? $userInfoData['item_list'][0]['statistics']['comment_count'] : '',
			'info_signature'=>isset($userInfoData['item_list'][0]['desc']) ? htmlspecialchars($userInfoData['item_list'][0]['desc'],ENT_QUOTES) : '',
			'info_fans'=>isset($userInfoData['item_list'][0]['statistics']['share_count']) ? $userInfoData['item_list'][0]['statistics']['share_count'] : '',
			'info_url'=>$getUrl,
			'info_vurl'=>$info_vurl,
			'info_date'=>date('Y-m-d H:i:s',time())
		);
		// echo "<pre/>";print_r($userInfoData);exit();
		$userInfo=new Userinfo();
		$values=array(2,$item_ids,$system_type,$partner_id);
		$returnId=$userInfo->insertUser($data,$values);
		
		$returnId ? $this->returnJson(0,'操作成功') : $this->returnJson(1,'操作失败');
	}
	
	//获取真实地址
	function getContents($url){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_VERBOSE, true);
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLOPT_NOBODY, true);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_TIMEOUT, 20);
		curl_setopt($ch, CURLOPT_AUTOREFERER, true);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  //是否抓取跳转后的页面
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不从证书中检查SSL加密算法是否存在
		$res = curl_exec($ch);
		$info = curl_getinfo($ch);
		$retURL = $info['url'];   // 跳转后的 URL 信息
		curl_close($ch);
		return $retURL;
	}
	/*截取指定位置字符串
	*	$str 要截取的字符串   $start 截取开始位置    $end 截取结束位置   $number 从第几个取   $fristStr 匹配字符串的位置
	*/
	function cut_str($str,$start,$end,$number){
		$fristStr=strpos($str,$start);
		if($fristStr===false){$this->returnJson(1,'网址错误');}
		$sta=mb_substr($str,$fristStr+$number);
		return mb_substr($sta,0,strpos($sta,$end));
	}
	//获取数据
	function douyinContents($url){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_TIMEOUT, 20);
		curl_setopt($ch, CURLOPT_AUTOREFERER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不从证书中检查SSL加密算法是否存在
		$res = curl_exec($ch);
		curl_close($ch);
		return json_decode($res,true);
	}
}